<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>表单</title>
    <link rel="stylesheet" href="__TPL__/frame/layui/css/layui.css">
    <link rel="stylesheet" href="__TPL__/frame/static/css/style.css">
    <link rel="icon" href="__TPL__/frame/static/image/code.png">
</head>
<body class="body">

<form class="layui-form" action="">

    <div class="layui-form-item">
        <label class="layui-form-label">用户名</label>
        <div class="layui-input-inline">
            <input type="text" name="uname" placeholder="请输入用户名" lay-verify="required|username" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">昵称</label>
        <div class="layui-input-inline">
            <input type="text" name="nickname" autocomplete="off" lay-verify="required"
                   placeholder="请输入昵称" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">用户组</label>
            <div class="layui-input-block">
                {volist name="groups" id="vo"}
                <input type="checkbox" name="group" lay-skin="primary" title="{$vo.name}" val="{$vo.id}">
                {/volist}
            </div>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">上级用户</label>
            <div class="layui-input-block" id="parent">
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">新增上级</label>
        <div class="layui-input-inline">
            <select lay-filter="parent_add">
                <option value="0">新增上级</option>
                {volist name="users" id="vo"}
                <option value="{$vo.id}">{$vo.nickname}</option>
                {/volist}
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit="" lay-filter="add_submit">新增用户</button>
        </div>
    </div>
</form>

<script src="__TPL__/frame/layui/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'jquery'], function () {
        var form = layui.form
            , layer = layui.layer
            , $ = layui.jquery;

        //监听提交
        form.on('submit(formEdit)', function (data) {
            layui.use(['jquery', 'layer'], function () {
                var $ = layui.$
                    , layer = layui.layer;

                data.field.role_ids = [];
                $("input:checkbox[name='role']:checked").each(function () { // 遍历name=week的多选框
                    data.field.role_ids.push($(this).val());  // 每一个被选中项的值
                });
                $.ajax({
                    url: "{:url('Auth/editUserPost')}",
                    data: data.field,
                    dataType: 'json',
                    type: 'post',
                    success: function (data) {
                        if (data.status == 1) {
                            parent.location.reload();
                        } else {
                            console.log(data.info);
                        }
                    }
                });
            });
            return false;
        });
        //事件监听
        form.on('select(parent_add)', function (opt) {
            if (opt.value != '0') {
                var id = opt.elem.value;
                var obj_list = $('[name=pid]');
                var id_list = [];
                obj_list.each(function () {
                    if ($(this).attr('val'))
                        id_list.push($(this).attr('val'));
                });
                console.log($.inArray(id, id_list));
                if ($.inArray(id, id_list) === -1) {
                    var text = opt.elem.selectedOptions[0].innerText;
                    var ipt_str = "<input type='checkbox' name='pid' lay-skin='primary' title='" + text + "' val='" + id + "' checked>";
                    $('#parent').after(ipt_str);
                    form.render();
                }else{
                    layer.msg('已存在此上级');
                }
            }
        });
        form.on('submit(add_submit)',function (data) {
//            console.log(data.field);
            var group_obj_list = $('[name=group]:checked');
            var pid_obj_list = $('[name=pid]:checked');
            var temp_group = [];
            group_obj_list.each(function () {
                temp_group.push($(this).attr('val'))
            });
            var temp_pid = [];
            pid_obj_list.each(function () {
                temp_pid.push($(this).attr('val'))
            });
            data.field.group = temp_group;
            data.field.pid = temp_pid;
            $.ajax({
                url:'{:url("Auth/addUserPost")}',
                type:'post',
                data:data.field,
                dataType:'json',
                success:function (data) {
                    if (data.code === 1){
                        var index=parent.layer.getFrameIndex(window.name);
                        parent.layer.close(index);
                    }else{
                        layer.msg(data.msg, {icon: 1, time: 2000})
                    }
                }
            });
            return false;
        })
        //表单验证
        form.verify({
            username: function(value, item){ //value：表单的值、item：表单的DOM对象
                if(!new RegExp(/^[a-zA-Z0-9_-]{4,16}$/).test(value)){
                    return '用户名不能有中文字符或特殊字符';
                }
                if(/(^\_)|(\__)|(\_+$)/.test(value)){
                    return '用户名首尾不能出现下划线\'_\'';
                }
            }
        });
    });
</script>
</body>
</html>